package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.MessageRemindBean;
import com.example.administrator.liveapp.bean.SearchPageBean;
import com.example.administrator.liveapp.utils.AppUrl;

import java.util.List;

/**
 * Created by Administrator on 2017/9/27 0027.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> implements View.OnClickListener {

    public SearchPageBean bean;
    public Context context;

    public SearchAdapter(SearchPageBean bean) {
        this.bean = bean;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_item, null);
        ViewHolder holder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(bean.getSearch().get(position).getCid().equals("3")){
            holder.action_item.setVisibility(View.GONE);
            holder.article_item.setVisibility(View.VISIBLE);
            holder.article_title.setText(bean.getSearch().get(position).getTitle());
            holder.article_name.setText(bean.getSearch().get(position).getAuthor());
            holder.article_column.setText(bean.getSearch().get(position).getColumn());
            holder.article_view.setText(bean.getSearch().get(position).getView());
            if(bean.getSearch().get(position).getImg()!=null&&!bean.getSearch().get(position).getImg().equals("")){
                String str = bean.getSearch().get(position).getImg();
                String photo = str.substring(0,4);
                if(photo.equals("http")){
                    Glide.with(context).load(bean.getSearch().get(position).getImg()).into(holder.article_image);
                }else{
                    Glide.with(context).load(AppUrl.URL+bean.getSearch().get(position).getImg()).into(holder.article_image);
                }
            }
        }else{
            holder.action_item.setVisibility(View.VISIBLE);
            holder.article_item.setVisibility(View.GONE);
            holder.textTitle.setText(bean.getSearch().get(position).getTitle());
            holder.textAuthor.setText(bean.getSearch().get(position).getAuthor());
            holder.column.setText(bean.getSearch().get(position).getColumn());
            holder.view.setText(bean.getSearch().get(position).getView());
            Glide.with(context).load(AppUrl.URL+bean.getSearch().get(position).getImg()).into(holder.activity_image);
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        if (bean.getSearch() == null) {
            return 0;
        } else {
            return bean.getSearch().size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textTitle,article_title;
        public TextView textAuthor,article_name;
        public TextView column,article_column;
        public TextView view,article_view;
        public ImageView activity_image,article_image;
        public LinearLayout action_item,article_item;

        public ViewHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.textTitle);
            article_title = (TextView) itemView.findViewById(R.id.article_title);
            textAuthor = (TextView) itemView.findViewById(R.id.textAuthor);
            article_name = (TextView) itemView.findViewById(R.id.article_name);
            column = (TextView) itemView.findViewById(R.id.column);
            article_column = (TextView) itemView.findViewById(R.id.article_column);
            view = (TextView) itemView.findViewById(R.id.view);
            article_view = (TextView) itemView.findViewById(R.id.article_view);
            activity_image = (ImageView) itemView.findViewById(R.id.activity_image);
            article_image = (ImageView) itemView.findViewById(R.id.article_image);
            action_item = (LinearLayout) itemView.findViewById(R.id.action_item);
            article_item = (LinearLayout) itemView.findViewById(R.id.article_item);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
